﻿@using OBUCare.Core.Models
@using OBUCare.Web.Models
@using OBUCare.Web.Infrastructure
@using OBUCare.Core.Common;
@{
    ViewBag.Title = "请确认需调整费用的维修标签信息";

    var AdjustTypes = new List<SelectListItem>();
    AdjustTypes.Add(new SelectListItem() { Text = "增加", Value = "" });
    AdjustTypes.Add(new SelectListItem() { Text = "减免", Value = "-",Selected=true });
}

<h2>@ViewBag.Title</h2>

@(Html.Telerik().Grid<CostModel>()
        .Name("Grid")
        .DataKeys(keys => keys.Add(c => c.RepairOrders.ID))        
        .DataBinding(dataBinding => dataBinding
            .Ajax()
            .Select("List", "Cost", new { sql = ViewBag.Sql })          
                    )
        .Columns(columns =>
        {
            columns.Bound(o => o.RepairOrders.ID)
                .ClientTemplate("<input name='checkedRecords' type='checkbox' checked='true' value='<#= Id #>' title='选中当前行'/> <#= ++index #>")
                .HeaderTemplate
                (
                    @<text>
                        <input type="checkbox" title="全选" id="checkAllRecords" />
                    </text>
                ).Width(80);
            columns.Bound(o => o.RepairOrders.REPAIR_ORDER_NO).Width(220);
            columns.Bound(o => o.RepairOrders.OBU_NO).Width(150);
            columns.Bound(o => o.RepairOrders.USER_NAME).Width(150);
            columns.Bound(o => o.RepairOrders.SUPPLIER_ShortName).Width(150);
            columns.Bound(o => o.RepairOrders.SPEC_NAME).Width(120);
            columns.Bound(o => o.RepairOrders.CREATED_DATE).Width(150);
            columns.Bound(o => o.RepairOrders.AGENT_NAME).Width(150);
            columns.Bound(o => o.RepairOrders.StatusTitle).Width(150);
            columns.Bound(o => o.RepairFeeTotal).Title("维修费(元)").Width(120);  
            columns.Bound(o => o.ServiceFeeTotal).Title("服务费(元)").Width(120);     
        }).TableHtmlAttributes(new { style = "width:1200px" })     
        .ClientEvents(events =>
        {     
            events.OnError("onError");
            events.OnDataBinding("onDataBinding");
        })        
        .Scrollable(scroll => scroll.Height(CommonStyleForUI.Grid_Height))
        //.Selectable()
)

<div>
    <table>
        <tr>
            <td>
                维修费：
            </td>
            <td>
                @Html.Telerik().DropDownList().Name("RepairFeeAdjustType").BindTo(AdjustTypes)
            </td>
            <td>
                @Html.Telerik().NumericTextBox().Name("RepairFeeAdjustAmount").Value(0).MinValue(0)
                (元)
            </td>
            <td>
                原因：
            </td>
            <td>
                @Html.TextBox("RepairFeeAdjustRemark")
            </td>
        </tr>

        <tr>
            <td>
                服务费：
            </td>
            <td>
                @Html.Telerik().DropDownList().Name("ServiceFeeAdjustType").BindTo(AdjustTypes)                   
            </td>
            <td>
                @Html.Telerik().NumericTextBox().Name("ServiceFeeAdjustAmount").Value(0).MinValue(0)
                (元)
            </td>
            <td>
                原因：
            </td>
            <td>
                @Html.TextBox("ServiceFeeAdjustRemark")
            </td>
        </tr>

    @*    <tr>
            <td>
                合计：
            </td>
            <td id='total'>
                            
            </td>
            <td>
                
            </td>
        </tr>*@
    </table>

    <p>
        <input type="button"  id="btnSubmit" value="提交" />
    </p>
</div>


<!--为全选框注册事件-->
@{ Html.Telerik().ScriptRegistrar().OnDocumentReady(@<text>
    /* attach event handler to "check all" checkbox */
        $('#checkAllRecords').click(function checkAll() {
            $("#Grid tbody input:checkbox").attr("checked", this.checked);            
        });
    </text>); }

<script type="text/javascript">
//    function DropDownList_OnChange(e) {
//        feeTotal();
//    }



    $().ready(function () {
        $("#btnSubmit").click(Submit);
//        feeTotal();
//        var repairFee = document.getElementById('RepairFeeAdjustAmount');
//        repairFee.onkeyup = function () {
//            feeTotal();
//        }
//        var serviceFee = $("#ServiceFeeAdjustAmount");
//        serviceFee.keyup(function () {
//            feeTotal();
//        })
    });

//    function feeTotal() {
//        var repairFee = $('#RepairFeeAdjustType').data('tDropDownList').value() + $("#RepairFeeAdjustAmount").val();

//        var serviceFee = $('#ServiceFeeAdjustType').data('tDropDownList').value() + $("#ServiceFeeAdjustAmount").val();
//        var total = parseFloat(repairFee) + parseFloat(serviceFee);
//        if (total > 0) {
//            $("#total").html("增加：" + total + "（元）");
//        }
//        else if (total < 0) {
//            $("#total").html("减免：" + total * (-1) + "（元）");
//        }
//        else {
//            $("#total").html("0（元）");
//        }
//    }

function onError(e) {
        //debugger;
        e.preventDefault();
        /*if (e.XMLHttpRequest.status == "505")
            alert("系统字段不允许删除");
        else if (e.XMLHttpRequest.status == "506")
            alert("系统表不允许删除");
        else*/
        alert(e.XMLHttpRequest.getResponseHeader("message"));
    }

    var index = 0;

    function onDataBinding() {
        var grid = $('#Grid').data('tGrid')
        index = (grid.currentPage - 1) * grid.pageSize;
    }

    function Submit()
    {
        //debugger;
        //获取被选中的ID
        var strBuff = new StringBuffer();
        $("#Grid tbody input:checkbox[name =checkedRecords][checked]").each(function () {
            strBuff.append(this.value);
        });
        strIDs = strBuff.arrayToString(",");
        if (strIDs.length < 1)
            alert("请选中要调整的维修单");
        else {
            if (checkRemark()) {
                var RepairFeeAdjustAmount = $('#RepairFeeAdjustType').data('tDropDownList').value() + $("#RepairFeeAdjustAmount").val();
                var RepairFeeAdjustRemark = $("#RepairFeeAdjustRemark").val();

                var ServiceFeeAdjustAmount = $('#ServiceFeeAdjustType').data('tDropDownList').value() + $("#ServiceFeeAdjustAmount").val();
                var ServiceFeeAdjustRemark = $("#ServiceFeeAdjustRemark").val();

                var jdata = {
                    strIDs: strIDs,
                    RepairFeeAdjustAmount: RepairFeeAdjustAmount,
                    RepairFeeAdjustRemark: RepairFeeAdjustRemark,
                    ServiceFeeAdjustAmount: ServiceFeeAdjustAmount,
                    ServiceFeeAdjustRemark: ServiceFeeAdjustRemark
                };
                $.post("/Cost/ConfirmAdjust", jdata,
                        function (data) {
                            // Successful requests get here
                            // Update the page elements

                            if (data.success) {
                                alert("调整成功");
                                window.close();
                                //window.location.href = "/RepairOrders/List/";
                            }
                            else {
                                alert("调整失败:" + data.message);
                            }
                        });
            }
        }
       
    }
    function checkRemark() {
        var RepairFeeAdjustAmount = $("#RepairFeeAdjustAmount").val();
        var RepairFeeAdjustRemark = $("#RepairFeeAdjustRemark").val();

        var ServiceFeeAdjustAmount = $("#ServiceFeeAdjustAmount").val();
        var ServiceFeeAdjustRemark = $("#ServiceFeeAdjustRemark").val();
        if (RepairFeeAdjustAmount > 0 && RepairFeeAdjustRemark.StringIsNullOrEmpty()) {
            alert("请填写调整维修费原因");
            return false;
        }
        if (ServiceFeeAdjustAmount > 0 && ServiceFeeAdjustRemark.StringIsNullOrEmpty()) {
            alert("请填写调整服务费原因")
            return false;
        }
        return true;
    }
</script>